package iscript.uiFramework.util.color
{
	
	/**
	 * @example
	 * var darkGreenHsbObj:Object = new Object();
			darkGreenHsbObj.h = 90;
			darkGreenHsbObj.s = 100;
			darkGreenHsbObj.b = 40;
	 */
	public class HSB
	{
		
		public function HSB()
		{
		
		}
		
		statpic public function toRGB(h:int, s:int, b:int):RGB
		{
			var rgb:RGB = new RGB();
			var max:Number = (b * 0.01) * 255;
			var min:Number = max * (1 - (s * 0.01));
			
			if (h == 360)
			{
				h = 0;
			}
			
			if (s == 0)
			{
				rgb.Red = rgb.Green = rgb.Blue = b * (255 * 0.01);
			}
			else
			{
				var _h:Number = Math.floor(h / 60);
				
				switch (_h)
				{
					case 0: 
						rgb.Red = max;
						rgb.Green = min + h * (max - min) / 60;
						rgb.Blue = min;
						break;
					case 1: 
						rgb.Red = max - (h - 60) * (max - min) / 60;
						rgb.Green = max;
						rgb.Blue = min;
						break;
					case 2: 
						rgb.Red = min;
						rgb.Green = max;
						rgb.Blue = min + (h - 120) * (max - min) / 60;
						break;
					case 3: 
						rgb.Red = min;
						rgb.Green = max - (h - 180) * (max - min) / 60;
						rgb.Blue = max;
						break;
					case 4: 
						rgb.Red = min + (h - 240) * (max - min) / 60;
						rgb.Green = min;
						rgb.Blue = max;
						break;
					case 5: 
						rgb.Red = max;
						rgb.Green = min;
						rgb.Blue = max - (h - 300) * (max - min) / 60;
						break;
					case 6: 
						rgb.Red = max;
						rgb.Green = min + h * (max - min) / 60;
						rgb.Blue = min;
						break;
				}
				
				rgb.Red = Math.min(255, Math.max(0, Math.round(rgb.Red)))
				rgb.Green = Math.min(255, Math.max(0, Math.round(rgb.Green)))
				rgb.Blue = Math.min(255, Math.max(0, Math.round(rgb.Blue)))
			}
			return rgb;
		}
	
	}

}